import Common from '../../common/common.js';
import TimNet from '../../timnet.js';
import {bigEmoji, emojiMap, emojiName, emojiUrl} from "../../common/emojiMap.js";
import {MessWin} from "./TalkWin.js";

function MessListItem(data,parent) {
    this.element;
    this.initElement();
    this.parent = parent;
    this.setData(data);
    console.log(data)

}

MessListItem.prototype = {
    initElement: function () {

        let li = Common.tool.createElement( { label: "li"});
        let avatarDiv = Common.tool.createElement( { props: {className: "img"}, childNodes: [ { tag:"avatar", label: "img"  } ]  });
        let info =  Common.tool.createElement({props: {className: "info"}, childNodes: [{ tag:"name", props: {className: "name"}  }, { tag:"message", props: {className: "message"}  }, { tag:"time",  props: {className: "time"} }  ] });

        li.avatar = avatarDiv.avatar;
        li.name = info.name;
        li.message = info.message;
        li.time = info.time;
        li.appendChild(avatarDiv);
        li.appendChild(info);

        this.element = li;
        this.bind();
    },
    setAvatar:function(avatar){
        this.element.avatar.src = Common.tool.getAvatar(avatar);
    },
    setName:function(name){
        this.element.name.innerText = name;
    },
    setTime:function(time){
        this.element.time.innerText = Common.tool.formatDateTime(time);
    },
    setData:function(data){
        this.data = data;
        this.element.className = data.flow;
        //显示通用部分
        if(data.from == TIM.TYPES.CONV_SYSTEM)
        {
            this.setName("系统通知");
            this.setAvatar("system");
         }else{
            //缓存用户
            if(Common.cache.userInfo[data.from]){
                const user = Common.cache.userInfo[data.from];
                this.setName(Common.tool.isNull(user.nick)?user.userID:user.nick);
                this.setAvatar(user.avatar);
            }else{
                TimNet.getUserProfile([data.from],(userlist)=>{
                    if(userlist.length > 0){
                        Common.cache.userInfo[userlist[0].userID] = userlist[0];
                        const user = userlist[0];
                        this.setName(Common.tool.isNull(user.nick)?user.userID:user.nick);
                        this.setAvatar(user.avatar);
                    }
                });
            }
         }
        this.setTime(data.time);

        //显示信息内容
        this.element.message.innerHTML="";
        switch (data.type) {
            case TIM.TYPES.MSG_TEXT:
                this.showTextMessage(data);
                break;
            case TIM.TYPES.MSG_FACE:
                this.showFaceMessage(data);
                break;
            case TIM.TYPES.MSG_IMAGE:
                this.showImageMessage(data);
                break;
            case TIM.TYPES.MSG_FILE:
                this.showFileMessage(data);
                break;
            case TIM.TYPES.MSG_VIDEO:
                this.showVideoMessage(data);
                break;
            //群消息
            case TIM.TYPES.MSG_GRP_TIP:
                var operatorID = data.payload.operatorID;
                var operationType = data.payload.operationType;

                var getText = (type)=>{
                    let user = Common.cache.userInfo[operatorID];
                    let text = "";
                    let name = "“"+(Common.tool.isNull(user.nick)?user.userID:user.nick) + "” "
                    switch (type) {
                        case TIM.TYPES.GRP_TIP_MBR_JOIN:
                            text = name + "入群";
                            break;
                        case TIM.TYPES.GRP_TIP_MBR_QUIT:
                            text =  name +"退群";
                            break;
                        case TIM.TYPES.GRP_TIP_MBR_KICKED_OUT:
                            text =  name +"被踢出群";
                            break;
                        case TIM.TYPES.GRP_TIP_MBR_SET_ADMIN:
                            text =  name +"被设为管理员";
                            break;
                        case TIM.TYPES.GRP_TIP_MBR_CANCELED_ADMIN:
                            text =  name +"被撤销管理员";
                            break;
                        case TIM.TYPES.GRP_TIP_GRP_PROFILE_UPDATED:
                            text =  "群组资料变更";
                            break;
                        case TIM.TYPES.GRP_TIP_MBR_PROFILE_UPDATED:
                            text =  "群成员资料变更";
                            break;
                        default:
                            text =  "自定义通知";
                            break;
                    }

                    return text;
                }

                if(Common.cache.userInfo[operatorID]){
                    this.showDefaultMessage( getText(operationType));
                }else{
                    TimNet.getUserProfile([operatorID],(userlist)=>{
                        Common.cache.userInfo[operatorID] = userlist[0];
                        this.showDefaultMessage( getText(operationType));
                    });
                }

                break;
            case TIM.TYPES.MSG_GRP_SYS_NOTICE:

                var operatorID = data.payload.operatorID;
                var operationType = data.payload.operationType;
                let groupName = data.payload.groupProfile.name;

                var getText = (type)=>{
                    let user = Common.cache.userInfo[operatorID];
                    let text = "";
                    switch (type) {
                        case 1:
                            text =  "申请加群";
                            break;
                        case 2:
                            text =  "申请加群被同意";
                            break;
                        case 3:
                            text =  "申请加群被拒绝";
                            break;
                        case 4:
                            text =  "被踢出群组";
                            break;
                        case 5:
                            text =  "群组被解散";
                            break;
                        case 6:
                            text =  "创建群组";
                            break;
                        case 7:
                            text =  "邀请加群";
                            break;
                        case 8:
                            text =  "退群";
                            break;
                        case 9:
                            text =  "设置管理员";
                            break;
                        case 10:
                            text =  "取消管理员";
                            break;
                        default:
                            text =  "自定义通知";
                            break;
                    }

                    return "“"+(Common.tool.isNull(user.nick)?user.userID:user.nick) + "” "+text +"[群组："+groupName+"]";
                }

                if(Common.cache.userInfo[operatorID]){
                     this.showDefaultMessage( getText(operationType));
                }else{
                    TimNet.getUserProfile([operatorID],(userlist)=>{
                        Common.cache.userInfo[operatorID] = userlist[0];
                        this.showDefaultMessage( getText(operationType));
                    });
                }

                break;
            default:
                this.showDefaultMessage("[消息类型不支持]");
                break;
        }
     },
    updateProgress:function(progress){
        if(this.data.isProgress){
            this.element.progress.innerText = progress+"%";
            this.element.progress.style.width = progress+"%";

        }
    },
    showVideoMessage:function(data){
        let video = Common.tool.createElement({
            label: "video",
            props: {
                controls: "controls",
                className: "face",
                src: data.payload.videoUrl,
                style: "max-width:300px;"
            }
        });
        this.element.message.appendChild(video);

        if(data.isProgress){
            let progress = Common.tool.createElement({
                props: {
                    className: "progress",
                    innerText: "0%",
                    style: "text-align: center;border-bottom: 2px solid #0e541c; width: 0%; font-size: 12px;white-space: nowrap;"
                }
            });
            this.element.progress = progress;
            this.element.message.appendChild(progress);
        }
    },
    showImageMessage:function(data){
        let imageInfo = data.payload.imageInfoArray.filter(t=>t.type == 1)[0];
        let image = Common.tool.createElement({
            label: "img",
            props: {
                className: "face",
                src: imageInfo.url,
                style: "max-width:300px;"
            }
        });
        this.element.message.appendChild(image);
        if(data.isProgress){
            let progress = Common.tool.createElement({
                props: {
                    className: "progress",
                    innerText: "0%",
                    style: "text-align: center;border-bottom: 2px solid #0e541c; width: 0%; font-size: 12px;white-space: nowrap;"
                }
            });
            this.element.progress = progress;
            this.element.message.appendChild(progress);
        }
    },
    showFileMessage:function(data){
        let fileInfo = Common.tool.getFileInfo(data.payload.fileName);
        let image = Common.tool.createElement({
            label: "img",
            props: {
                className: "face",
                src: fileInfo.icon,
                style: "max-width:300px;"
            }
        });
        let fileName = Common.tool.createElement({
            props: {
                className: "text",
                innerText: fileInfo.name,
                style: "text-align: center;width: 72px; font-size: 12px;"
            }
        })
        this.element.message.appendChild(image);
        this.element.message.appendChild(fileName);
        if(data.isProgress){
            let progress = Common.tool.createElement({
                 props: {
                     className: "progress",
                     innerText: "0%",
                     style: "text-align: center;border-bottom: 2px solid #0e541c; width: 0%; font-size: 12px;white-space: nowrap;"
                 }
         });
            this.element.progress = progress;
            this.element.message.appendChild(progress);
        }
    },
    showFaceMessage:function(data){
        this.element.message.appendChild(Common.tool.createElement( {
            label: "img",
            props: {
                className: "face",
                src: bigEmoji[data.payload.index],
                style: "max-width:150px;"
            }
        }));
    },
    showTextMessage:function(data){
        this.element.message.appendChild(Common.tool.createElement( {
            props: {className: "text", innerHTML: this.textToEmoji(data.payload.text)}
        }));
    },
    showDefaultMessage:function(text){
        this.element.message.appendChild(Common.tool.createElement( {
            props: {className: "text", innerHTML: text}
        }));
    },
    textToEmoji:function(text){
        for (let string of emojiName) {
            text = text.replace(new RegExp(string.replace("\[", "\\[").replace("\]", "\\]"), "g"), "<img src='" + emojiUrl + emojiMap[string] + "'/>");
        }
        return text;
    },
    remove:function(){
        this.element.parentElement.removeChild(this.element);
    },
    bind:function(){

        Common.tool.addHandle(this.element.message,"click",(e)=>{
            new MessWin(e,this).create()
        });
    }
}

export default MessListItem;
